package com.smartcar.client.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.remote.server.feigin.OrderServiceFeigin;
import com.smartcar.client.domin.*;
import com.smartcar.client.mapper.ClientMapper;
import com.smartcar.client.service.ClientService;
import com.smartcar.client.util.MapUtil;
import com.smartcar.client.vo.AnnexVo;
import com.smartcar.client.vo.ConsumptionVo;
import com.smartcar.client.vo.RechargeVo;
import com.smartcar.common.constants.TokenConstants;
import com.smartcar.common.domain.User;
import com.smartcar.common.domain.request.OrderRequest;
import com.smartcar.common.result.PageResult;
import com.smartcar.common.result.Result;
import com.smartcar.common.utils.JwtUtils;
import com.smartcar.common.utils.SnowFlakeUtil;
import com.smartcar.common.utils.TelSmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author : 高文娜大美女
 * @className : ClientServiceImpl
 * @description : 描述说明该类的功能
 * @createTime : 2023/10/15 10:26
 */
@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private OrderServiceFeigin orderServiceFeigin;
    @Autowired
    private MapUtil mapUtil;


    /**
     * 获取用户信息
     * @return
     */
    public User userInfo() {
        // 拿到token令牌
        String token = request.getHeader(TokenConstants.TOKEN);
        // 取出userKey
        String userKey = JwtUtils.getUserKey(token);
        // 从redis中取出用户信息 --> json字符串
        String jsonStr = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        // 返回解析的用户信息
        User user = JSONObject.parseObject(jsonStr, User.class);

        return user;
    }

    @Override
    public Annex findByAnnex(AnnexVo annexVo) {
        Annex findByAnnex=clientMapper.findByAnnex(annexVo);
        return findByAnnex;
    }

    @Override
    public PageInfo<Consumption> findByConsumption(ConsumptionVo consumptionVo) {
        PageHelper.startPage(consumptionVo.getPageNum(),consumptionVo.getPageSize());
        List<Consumption> findByConsumption=clientMapper.findByConsumption(consumptionVo);
        return new PageInfo<>(findByConsumption);
    }

    @Override
    public  PageInfo<Recharge>  findByRecharge(RechargeVo rechargeVo) {
        PageHelper.startPage(rechargeVo.getPageNum(),rechargeVo.getPageSize());
       List<Recharge> findByRecharge= clientMapper.findByRecharge(rechargeVo);
        PageInfo<Recharge> info = new PageInfo<>(findByRecharge);
        return info;
    }

    @Override
    public Vip findByVipId(String userId) {
        Vip findByVipId=clientMapper.findByVipId(userId);
        return findByVipId;
    }

    @Override
    public Integer insertAnnex(Annex annex) {
       Integer i= clientMapper.insertAnnex(annex);
       return i;
    }

    @Override
    public Integer topUp(Annex annex) {

      Integer i=  clientMapper.topUp(annex);
      return i;
    }

    @Override
    public Integer insertVip(Vip vip) {

       Integer i= clientMapper.insertVip(vip);
       return i;
    }

    private static final Integer TEN=10;
    private static final Integer TWENTY=20;
    private static final Integer THIRTY=30;
    private static final Integer FORTY=40;
    private static final Integer FIFTY=50;
    private static final Integer SIXTY=60;
    @Override
    public Integer insertOrder(OrderRequest orderRequest) {
        orderRequest.setOrderNumber(SnowFlakeUtil.getDefaultSnowFlakeId() + "_" + System.currentTimeMillis());
        orderRequest.setOrderdownTime(new Date());
        User user = userInfo();
        orderRequest.setPersonPhone(user.getPhone());
        orderRequest.setUserName(user.getNickName());
        orderRequest.setOrderState(1);
        orderRequest.setOrderdownTime(new Date());
        Integer orderCarType = orderRequest.getOrderCarType();
        BigDecimal orderMoney = orderRequest.getOrderMoney();
        switch (orderCarType){
            case 1:
                orderMoney.add(BigDecimal.valueOf(TEN));
                break;
            case 2:
                orderMoney.add(BigDecimal.valueOf(TWENTY));
                break;
            case 3:
                orderMoney.add(BigDecimal.valueOf(THIRTY));
                break;
            case 4:
                orderMoney.add(BigDecimal.valueOf(FORTY));
                break;
            case 5:
                orderMoney.add(BigDecimal.valueOf(FIFTY));
                break;
            case 6:
                orderMoney.add(BigDecimal.valueOf(SIXTY));
                break;
        }
        orderRequest.setOrderMoney(orderMoney);
        orderServiceFeigin.insertUserOrder(orderRequest);
        //下单成功 发短信
        HashMap<String, String> map = new HashMap<>();
        map.put("data","尊敬的+"+user.getNickName()+"您于"+new Date()+"下单成功");
        TelSmsUtils.sendSms(user.getPhone(),"111111",map);
        return 1;
    }

    @Override
    public Destination address(Destination destination) {
        Destination destination1 = mapUtil.show3(destination);
        return destination1;

    }

}
